package cn.fl.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.comm.facade.framework.dto.SysDictionaryDTO;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.framework.constant.DictionaryConstant;
import cn.fl.system.dto.SysFlCompInfoDTO;
import cn.fl.system.service.ISysFlCompInfoApiService;
import cn.fl.system.service.ISysFlCompInfoService;
import cn.fl.system.transform.SysFlCompInfoVOTransform;
import cn.fl.system.vo.SysFlCompInfoVO;

@Service
public class SysFlCompInfoServiceImpl implements ISysFlCompInfoService {

	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(SysFlCompInfoServiceImpl.class);
	
	/**
	 * 系统-公司API暴露服务
	 */
	@Autowired
	private ISysFlCompInfoApiService sysFlCompInfoApiService;
	
	/**
	 * 本地数据字典服务
	 */
	@Autowired
	private ISysDictionaryApiService sysDictionaryApiService;

	@Override
	public SysFlCompInfoVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "selectById", "根据主键查询融资租赁公司失败,主键参数为空.ID:"+id,CurrentThreadContext.getCurrentUserName());
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		}
		SysFlCompInfoDTO dto = this.sysFlCompInfoApiService.selectSysFlCompInfoById(id,CurrentThreadContext.getCurrentSysCd(),CurrentThreadContext.getCurrentOrgCd());
		return SysFlCompInfoVOTransform.toVO(dto);
	}

	@Override
	public Long add(SysFlCompInfoVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "add", "新增融资租赁公司失败,融资租赁公司对象为空.DATA:"+JSONObject.toJSONString(entity),CurrentThreadContext.getCurrentUserName());
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		};
		return this.sysFlCompInfoApiService.addSysFlCompInfo(SysFlCompInfoVOTransform.toDTO(entity));
	}
	@Override
	public boolean removeSysFlCompInfoByIds(List<Long> ids)
			throws BaseException {
		if(CollectionUtils.isEmpty(ids)){
			BaseLogUtils.info(logger, "removeSysFlCompInfoByIds", "删除融资租赁公司失败 ,主键参数为空.IDS:"+JSONObject.toJSONString(ids),CurrentThreadContext.getCurrentUserName());
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		};
		return this.sysFlCompInfoApiService.removeSysFlCompInfoByIds(ids);
	}
	
	@Override
	public boolean modifySysFlCompInfo(SysFlCompInfoVO entity)
			throws BaseException {
		if(null == entity){
			BaseLogUtils.info(logger, "modifySysFlCompInfo", "修改融资租赁公司失败,融资租赁公司对象为空.DATA:"+JSONObject.toJSONString(entity),CurrentThreadContext.getCurrentUserName());
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		};
		return this.sysFlCompInfoApiService.modifySysFlCompInfo(SysFlCompInfoVOTransform.toDTO(entity));
	}


	/*@Override
	public boolean removeById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id))return false;
		return this.sysFlCompInfoApiService.removeById(id);
	}*/
	@SuppressWarnings("unchecked")
	@Override
	public Pager searchListPage(Pager page) throws BaseException{
		if(null == page)page = new Pager();
		page = this.sysFlCompInfoApiService.searchSysFlCompInfoListPage(page);
		List<SysFlCompInfoDTO> list = (List<SysFlCompInfoDTO>)page.getResultList();
		page.setResultList(SysFlCompInfoVOTransform.toVOList(list));
		return page;
		
	}

	@Override
	public List<KeyValueVO> searchCompPropCdList() throws BaseException {
		return this.getKvList(DictionaryConstant.CODE_GRP_COMP_PROP_CD);
		
	}

	@Override
	public List<KeyValueVO> searchCompTypCdList() throws BaseException {
		return this.getKvList(DictionaryConstant.CODE_GRP_COMP_TYP_CD);
	}

	@Override
	public List<KeyValueVO> searchCompLvlCdList() throws BaseException {
		return this.getKvList(DictionaryConstant.CODE_GRP_COMP_LVL_CD);
	}
	
	@Override
	public List<KeyValueVO> searchEmpQtyCdList() throws BaseException {
		return this.getKvList(DictionaryConstant.CODE_GRP_EMP_QTY_CD);
	}	
	
	/**
	 * 根据字典中代码组值获取组员集合
	 * @param grpCd 代码组值
	 * @return 集合
	 */
	private List<KeyValueVO> getKvList(String grpCd){
		if(StringUtils.isBlank(grpCd)){
			return null;
		}
		List<KeyValueVO> kvList = new ArrayList<>();
		try {
			List<SysDictionaryDTO> sysDictionaryDTOList = this.sysDictionaryApiService.searchGroup(CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd(), grpCd);
			if(CollectionUtils.isNotEmpty(sysDictionaryDTOList)){
				for (SysDictionaryDTO sysDictionaryDTO : sysDictionaryDTOList) {
					KeyValueVO keyValueVO = new KeyValueVO();
					keyValueVO.setKey(sysDictionaryDTO.getCode());
					keyValueVO.setValue(sysDictionaryDTO.getCodeNm());
					kvList.add(keyValueVO);
				}
			}
		} catch (BaseException e) {
			BaseLogUtils.info(logger, "getKvList", "获取字典项组"+grpCd+"失败.ERROR:"+e.getMessage(), CurrentThreadContext.getCurrentUserName());
		} catch (Exception e) {
			BaseLogUtils.error(logger, "getKvList", "获取字典项组"+grpCd+"失败,系统错误.ERROR:"+e.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return kvList;
	}

	@Override
	public boolean modifyEnAbleSysFlCompInfo(SysFlCompInfoVO entity)
			throws BaseException {
		if(null == entity){
			BaseLogUtils.info(logger, "modifyEnAbleSysFlCompInfo", "启用融资租赁公司失败,融资租赁公司对象为空.DATA:"+JSONObject.toJSONString(entity),CurrentThreadContext.getCurrentUserName());
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		};
		return this.sysFlCompInfoApiService.modifyEnAbleSysFlCompInfo(SysFlCompInfoVOTransform.toDTO(entity));
	}

	@Override
	public boolean modifyDisAbleSysFlCompInfo(SysFlCompInfoVO entity)
			throws BaseException {
		if(null == entity){
			BaseLogUtils.info(logger, "modifyDisAbleSysFlCompInfo", "禁用融资租赁公司失败,融资租赁公司对象为空.DATA:"+JSONObject.toJSONString(entity),CurrentThreadContext.getCurrentUserName());
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		};
		return this.sysFlCompInfoApiService.modifyDisAbleSysFlCompInfo(SysFlCompInfoVOTransform.toDTO(entity));
	
	}
}
